﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpressHelps.Model;
using MySql.Data.MySqlClient;

namespace ExpressHelps.DAL
{
    //这个类用来存放数据库的加载，主要是读出最大的taskId，再多次打开项目时防止重复，在load中要调用这个方法
    public static class DBLoad
    {
        public static string mysqlStr = "userid=zero_register;password=bd0247e3a0cc4ea0;server=mysql.sqlpub.com;database=express_helps;";
        public static void QuaryTaskId()
        {
            int taskId = 0;
            string sql = "select max(task_id) from tasks";
            using(MySqlConnection connection = new MySqlConnection(mysqlStr))
            {
                try
                {
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    MySqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        taskId = reader.GetInt32(0);
                        EventMediator.TriggerLogEvent($"最大的订单号为：{taskId}");
                    }
                }
                catch(Exception ex)
                {
                    EventMediator.TriggerLogEvent("数据库中暂时没有订单");
                }
            }
            LoginUsers.taskId = taskId + 1;
        }
    }
}
